package jk.spider.core.storage.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jk.spider.core.storage.ResInfoDAO;
import jk.spider.model.ResInfo;

import org.apache.log4j.Logger;

public class ResInfoDAOImpl implements ResInfoDAO {
	protected static final Logger log = Logger.getLogger(ResInfoDAOImpl.class);
	protected DAOHelp daoHelp;
	protected Map<Integer, ResInfo> infoMap;
	protected volatile ResInfo resInfo = null;

	public ResInfoDAOImpl(DAOHelp daoHelp) {
		this.daoHelp = daoHelp;
		infoMap = new HashMap<Integer, ResInfo>();
	}

	public void create(ResInfo info) {
		StringBuffer sql = new StringBuffer();
		sql.append("insert into spider_resource_info (rid, site, iType, city) values ( ");
		sql.append(daoHelp.format(info.getRid()) + ",");
		sql.append(daoHelp.format(info.getSite()) + ",");
		sql.append(daoHelp.format(info.getType()) + ",");
		sql.append(daoHelp.format(info.getCity()) + ")");
		daoHelp.execSql(sql.toString());

	}

	public void deleteAll() {
		String sql = "delete from spider_resource_info ";
		daoHelp.execSql(sql);
	}

	public ResInfo findById(int rid) {
		String _type = "";
		int _paId = 0;
		Statement stmt = null;
		String sql = "select rid, paid, type from spider_resource where rid = " + rid;
		ResultSet rs = null;
		try {
			stmt = daoHelp.getConnection().createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				_type = rs.getString("type");
				_paId = rs.getInt("paId");
			}

		} catch (SQLException e) {
			log.error("SQLException -> ", e);
			daoHelp.safeClose(rs);
			daoHelp.safeClose(stmt);
		} finally {
			daoHelp.safeClose(rs);
			daoHelp.safeClose(stmt);
		}

		if (_type.equals("seed")) {
			resInfo = getInfo(rid);
		} else if (_type.equals("monitor")) {
			resInfo = getInfo(rid);
		} else {
			findById(_paId);
		}
		if(resInfo == null) {
			System.out.print(" RES NULL ");
		}
		return resInfo;
	}
	
	private ResInfo getInfo(int rid) {
		ResInfo resInfo = null;
		
		resInfo = infoMap.get(rid);
		if(resInfo != null)
			return resInfo;
		
		Statement stmt = null;
		String sql = "select rid, site,itype,city from spider_resource_info where rid = " + rid;
		ResultSet rs = null;
		try {
			stmt = daoHelp.getConnection().createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				resInfo = new ResInfo();
				resInfo.setSite(rs.getString("site"));
				resInfo.setType(rs.getString("iType"));
				resInfo.setCity(rs.getInt("city"));
			}
			daoHelp.safeClose(rs);
			daoHelp.safeClose(stmt);
		} catch (SQLException e) {
			log.error("SQLException -> ", e);
			daoHelp.safeClose(rs);
			daoHelp.safeClose(stmt);
		} finally {
			daoHelp.safeClose(rs);
			daoHelp.safeClose(stmt);
		}
		return resInfo;
	}

	public void init() {
		Statement stmt = null;
		String sql = "select top 5000 rid, site,itype,city from spider_resource_info ";
		ResultSet rs = null;
		try {
			stmt = daoHelp.getConnection().createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				ResInfo resInfo = new ResInfo();
				resInfo.setSite(rs.getString("site"));
				resInfo.setType(rs.getString("iType"));
				resInfo.setCity(rs.getInt("city"));
				infoMap.put(rs.getInt("rid"), resInfo);
			}
			daoHelp.safeClose(rs);
			daoHelp.safeClose(stmt);
		} catch (SQLException e) {
			log.error("SQLException -> ", e);
			daoHelp.safeClose(rs);
			daoHelp.safeClose(stmt);
		} finally {
			daoHelp.safeClose(rs);
			daoHelp.safeClose(stmt);
		}
	}
}
